package com.ruoyi.petition.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.petition.domain.LegalClassroom;
import com.ruoyi.petition.mapper.LegalClassroomMapper;
import com.ruoyi.petition.service.ILegalClassroomService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * 法制讲堂Service业务层处理
 *
 * @author ZWX
 * @date 2025-07-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class LegalClassroomServiceImpl extends ServiceImpl<LegalClassroomMapper, LegalClassroom> implements ILegalClassroomService {
    private final LegalClassroomMapper legalClassroomMapper;

    @Value("${ruoyi.profile}")
    private String profile;

    /**
     * 查询法制讲堂
     *
     * @param id 法制讲堂主键
     * @return 法制讲堂
     */
    @Override
    public LegalClassroom selectLegalClassroomById(Long id) {
        return legalClassroomMapper.selectLegalClassroomById(id);
    }

    /**
     * 查询法制讲堂列表
     *
     * @param legalClassroom 法制讲堂
     * @return 法制讲堂
     */
    @Override
    public List<LegalClassroom> selectLegalClassroomList(LegalClassroom legalClassroom) {
        return legalClassroomMapper.selectLegalClassroomList(legalClassroom);
    }

    /**
     * 新增法制讲堂
     *
     * @param legalClassroom 法制讲堂
     * @return 结果
     */
    @Override
    public int insertLegalClassroom(LegalClassroom legalClassroom) {
        legalClassroom.setCreateTime(DateUtils.getNowDate());
        setLegalClassroom(legalClassroom);
        return legalClassroomMapper.insertLegalClassroom(legalClassroom);
    }

    /**
     * 修改法制讲堂
     *
     * @param legalClassroom 法制讲堂
     * @return 结果
     */
    @Override
    public int updateLegalClassroom(LegalClassroom legalClassroom) {
        legalClassroom.setUpdateTime(DateUtils.getNowDate());
        setLegalClassroom(legalClassroom);
        return legalClassroomMapper.updateLegalClassroom(legalClassroom);
    }

    /**
     * 批量删除法制讲堂
     *
     * @param ids 需要删除的法制讲堂主键
     * @return 结果
     */
    @Override
    public int deleteLegalClassroomByIds(Long[] ids) {
        return legalClassroomMapper.deleteLegalClassroomByIds(ids);
    }

    /**
     * 删除法制讲堂信息
     *
     * @param id 法制讲堂主键
     * @return 结果
     */
    @Override
    public int deleteLegalClassroomById(Long id) {
        return legalClassroomMapper.deleteLegalClassroomById(id);
    }

    /**
     * 处理法制讲堂数据
     *
     * @param legalClassroom 法制讲堂
     */
    private void setLegalClassroom(LegalClassroom legalClassroom) {
        String videoUrl = legalClassroom.getVideoUrl();
        if (StrUtil.isNotBlank(videoUrl)) {
            String videoPath = videoUrl.replace(Constants.RESOURCE_PREFIX, profile);
            File videoFile = new File(videoPath);
            legalClassroom.setVideoSize(videoFile.length());
            
            // 获取视频时长
            try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoPath)) {
                grabber.start();
                long duration = grabber.getLengthInTime() / 1000000; // 转换为秒
                legalClassroom.setVideoTime(duration);
                grabber.stop();
            } catch (Exception e) {
                log.error("获取视频时长失败: {}", e.getMessage());
                legalClassroom.setVideoTime(0L);
            }
        }
    }
}
